﻿using EMS.system.Entityies.BaseData;
using EMS.system.IEntityAppService.BaseData.IPermissionService;
using EMS.system.IEntityAppService.BaseData.IPermissionService.PermissionDto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using static OpenIddict.Abstractions.OpenIddictConstants;

namespace EMS.system.EntityAppService.BaseData
{
    public class PermissionAppService: ApplicationService, IPermissionAppService
    {
        private readonly IRepository<Permission> _repository;

        public PermissionAppService(IRepository<Permission> repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 权限新增
        /// </summary>
        /// <param name="addDto"></param>
        /// <returns></returns>
        public async Task<int> PermissionAdd(PermissionAddDto addDto)
        {
            Permission permission = await _repository.InsertAsync(ObjectMapper.Map<PermissionAddDto, Permission>(addDto));
            return permission == null ? 0 : 1;
        }

        /// <summary>
        /// 显示权限
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<List<Permission>> PermissionAllList()
        {
            List<Permission> permissions = await _repository.GetListAsync();
            return permissions;
        }

        /// <summary>
        /// 显示权限
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<List<PermissionSelDto>> PermissionList()
        {
            List<Permission> permissions = await _repository.GetListAsync();
            List<PermissionSelDto> selDtos = PermissionDigui(permissions,0);
            return selDtos;
        }

        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="permissions"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<PermissionSelDto> PermissionDigui(List<Permission> permissions, int pid)
        {
            var permissions1 = permissions.Where(x=>x.UpPermissionId == pid);
            List<PermissionSelDto> selDtos = new List<PermissionSelDto>();
            foreach (Permission permission in permissions1)
            {
                PermissionSelDto permissionSel = new PermissionSelDto();
                permissionSel.Id = permission.Id;
                permissionSel.title = permission.PermissionName;
                permissionSel.children = PermissionDigui(permissions, permission.Id);
                selDtos.Add(permissionSel);
            }
            return selDtos;
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> PermissionDel(string ids)
        {
            await _repository.DeleteAsync(x => ids.Contains(x.Id.ToString()));
            return 1;
        }

        /// <summary>
        /// 获取单个权限信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PermissionUpdDto> GetSingPermission(int id)
        {
            Permission permissions = await _repository.FirstOrDefaultAsync(x => x.Id == id);
            return ObjectMapper.Map<Permission, PermissionUpdDto>(permissions);
        }

        /// <summary>
        /// 编辑权限信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> PermissionUpd(PermissionUpdDto dto)
        {
            Permission permission = ObjectMapper.Map<PermissionUpdDto, Permission>(dto);
            Permission permissionup = await _repository.UpdateAsync(permission);
            return permissionup == null ? 0 : 1;
        }
    }
}
